package com.flyco.modules.stm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.util.FileUtil;
import com.flyco.modules.stm.entity.StmChannelInfo;
import com.flyco.modules.stm.entity.StmChannelProductApply;
import com.flyco.modules.stm.entity.StmChannelProductApplyItem;
import com.flyco.modules.stm.mapper.stmChannelProductApplyItemMapper;
import com.flyco.modules.stm.service.IStmChannelInfoService;
import com.flyco.modules.stm.service.IStmChannelProductApplyService;
import com.flyco.modules.stm.service.IStmChannelProductApplyItemService;
import com.flyco.modules.system.entity.SysMail;
import com.flyco.modules.system.entity.SysUser;
import com.flyco.modules.system.service.ISysMailService;
import com.flyco.modules.system.service.ISysPermissionService;
import com.flyco.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 申请明细表
 * @Author: flyco
 * @Date: 2023-06-21
 * @Version: V1.0
 */
@Service
public class StmChannelProductApplyItemServiceImpl extends ServiceImpl<stmChannelProductApplyItemMapper, StmChannelProductApplyItem> implements IStmChannelProductApplyItemService {

    @Autowired
    private IStmChannelInfoService channelInfoService;
    @Autowired
    private ISysUserService userService;
    @Value(value = "${spring.mailTemp.path}")
    private String path;
    @Autowired
    private IStmChannelProductApplyService applyService;
    @Autowired
    private ISysMailService mailService;
    @Autowired
    private ISysPermissionService permissionService;

    @Override
    public Integer getApproveStatus(Integer applyId) {

        LambdaQueryWrapper<StmChannelProductApplyItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StmChannelProductApplyItem::getApplyId, applyId)
                .eq(StmChannelProductApplyItem::getStatus, 3);

        LambdaQueryWrapper<StmChannelProductApplyItem> allWrapper = new LambdaQueryWrapper<>();
        allWrapper.eq(StmChannelProductApplyItem::getApplyId, applyId);

        LambdaQueryWrapper<StmChannelProductApplyItem> approving = new LambdaQueryWrapper<>();
        approving.eq(StmChannelProductApplyItem::getApplyId, applyId)
                .eq(StmChannelProductApplyItem::getStatus, 2);

        Integer passCount = count(queryWrapper);
        Integer allCount = count(allWrapper);
        Integer approvingCount = count(approving);

        if (passCount == allCount) {
            return 3;
        } else if (approvingCount > 0) {
            return 2;
        } else {
            return 1;
        }
    }

    @Override
    public void sendMailForUpdatePrice(Integer applyId) {

        StmChannelProductApply applyInfo = applyService.getById(applyId);
        if (!applyInfo.getStatus().equals(1) && !applyInfo.getStatus().equals(2)) {
            return;
        }
        StmChannelInfo channelInfo = channelInfoService.getById(applyInfo.getChannelId());

        String content = channelInfo.getName() + "-" + applyInfo.getUserName() + "-申请ID" + applyInfo.getApplyId();
        List<SysUser> userList = new ArrayList<>();
        String subject = "江心味业订货平台渠道价格审核通知";
        String realPath = path + "/channelApplyApproveMailContent.html";
        switch (applyInfo.getStatus()) {
            case 1:
                //供应链审核
                userList = permissionService.getUserListByPerms("channel:supplyChainApprove");
                break;
            case 2:
                //财务审核
                userList = permissionService.getUserListByPerms("channel:financeApprove");
                break;
        }
        if (CollectionUtils.isEmpty(userList)) {
            return;
        }
        String mails = userList.stream().filter(o -> !StringUtils.isEmpty(o.getEmail())).map(o -> o.getEmail()).collect(Collectors.joining(";"));
        if (StringUtils.isEmpty(mails)) {
            return;
        }
        SysMail mail = new SysMail();
        String tempContent = FileUtil.readFile(realPath);
        tempContent = tempContent.replaceAll("%s", content);
        mail.setContent(tempContent);
        mail.setReceive(mails);
        mail.setBusinessType("channelApplyApprove");
        mail.setSubject(subject);
        mail.setSourceId(applyId.toString());
        mailService.sendMail(mail);

    }

}
